package com.mhy.netsafe.base;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.view.View;

import com.mhy.netsafe.MyApplication;
import com.mhy.netsafe.R;
import com.mhy.toast.ToastUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.squareup.leakcanary.RefWatcher;


/**
 * author    : mahongyin
 * e-mail    : mhy.work@qq.com
 * date      : 2019/7/11 14:05
 * introduce :
 */

public abstract class BaseMvpFragment<V, T extends BasePresenter<V>> extends Fragment {

    protected MyApplication mineApplication;
    protected T presenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mineApplication = (MyApplication) getActivity().getApplication();
    }

    protected abstract void initData();

    protected abstract void initViews();

    /**
     * 适配为不同的view 装载不同的presenter
     * @return
     */
    protected abstract T initPresenter();

    protected void setTopBar(QMUITopBar topbar) {
        topbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorWhiteA));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(getActivity());
        //设置状态栏黑色字体和图标，
        //支持4.4以上的MIUI和flyme  以及5.0以上的其他android
        QMUIStatusBarHelper.setStatusBarLightMode(getActivity());

    }

    protected void showToast(String message) {
        ToastUtils.show(message);
    }

    protected void startActivity(Class<?> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.dettachView();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ToastUtils.cancel();
        RefWatcher refWatcher = MyApplication.getRefWatcher(getActivity());
        refWatcher.watch(this);
    }
}
